package com.system.manager.modules.risk.controller;

import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.Result;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.risk.dto.ExamVo;
import com.system.manager.modules.risk.entity.TbExam;
import com.system.manager.modules.risk.service.ITbExamService;
import com.system.manager.modules.risk.vo.ExamDate;
import com.system.manager.modules.risk.vo.UserDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxp
 * @since 2021-11-11
 */
@Slf4j
@Api(tags = "试题功能")
@RestController
@RequestMapping("/risk/tb-exam/")
public class TbExamController {

    @Autowired
    private ITbExamService iTbExamService;

    //@Anonymous
    @ApiOperation("推送试题")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @GetMapping("exam")
    public Result examList(@RequestParam(name = "page", required = false, defaultValue = "1")Integer page,
                           @RequestParam(name = "limit", required = false, defaultValue = "10")Integer size,
                           @CurrSubject Subject subject){
        UserDate<TbExam> exam = iTbExamService.findExam(page,size,subject);

        return Result.ok(exam.getData(),exam.getCount());
    }


    @ApiOperation("做完，上传试题")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @PostMapping("postExam")
    public Result postExam(@RequestBody ArrayList<ExamVo> examVo,@CurrSubject Subject subject){
        iTbExamService.postExam(examVo,subject);
        UserDate<ExamDate> correct = iTbExamService.correct(examVo,subject);
        return new Result(correct.getCode(), correct.getMsg(),correct.getData(),correct.getCount());
    }


    @ApiOperation("查看自己的考试分数")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @PostMapping("findScore")
    public Result findScore(@CurrSubject Subject subject){
        UserDate score = iTbExamService.findScore(subject);
        return new Result(score.getCode(), score.getMsg(),score.getData(),score.getCount());
    }

    @ApiOperation("考卷分析：我的卷子")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @PostMapping("correct")
    public Result correct(@RequestBody ArrayList<ExamVo> examVo,@CurrSubject Subject subject){
        UserDate<ExamDate> correct = iTbExamService.correct(examVo,subject);
        return new Result(correct.getCode(), correct.getMsg(),correct.getData(),correct.getCount());
    }
}
